
	$(function(){
		//表单验证使用属性validate进行验证
		$.metadata.setType("attr","validate");
		$("#register_form").validate({
			success: function(label) {
				label.text("").addClass("validate_success");
			},
			rules: {
				babyName: {
					required: true
				},
				username: {
					required: true,
					maxlength: 5,
					chinese:5,
					minlength:2
				},
				email:{
					required: true,
					email:true,
					remote: 'checkEmail.ajax'
				},
				password:{
					required: true,
					minlength:6,
					maxlength:20
				},
				babyName1:{
					required:true,
					minlength:2,
					maxlength:5,
					chinese:24
				}
				
			},
			messages: {
				babyName: {
					required: "宝宝姓名不能为空"
				},
				username: {
					required:	"请填写你的真实姓名",
					minlength: "请填写2个以上中文名",
					maxlength: "请填写5个以下中文名",
					chinese: "请填写你的中文名"
				},
				email:{
					required: "请填写邮箱",
					email:"请填写正确的邮箱地址",
					remote: "该邮箱已被注册"
				},
				password:{
					required: "请填写密码",
					minlength:"密码不足6位，重新输入",
					maxlength:"密码不超过20位"
				},
				babyName1:{
					required:'请填写宝贝的真实名字',
					minlength:'请填写2个以上中文',
					maxlength:'请填写5个以下中文',
					chinese:'请填写2个以上中文'
				}
			},
			//提交由submitHandler拦截
			submitHandler:function() {
				$("#register_add_family").attr("disabled",true); 
				var family = new FamilyAll();
				family.nickname = $("#nickname").val();
				family.province = $("#province").val();
				family.city = $("#city").val();
				family.district = $("#district").val();
				family.email = $("#email").val();
				family.username = $("#username").val();
				family.role = $(":radio[name='role'][checked]").val();
				family.password = $("#password").val();
				var arrayObj = new Array();
				$(".register_baby_info_table").each(function(){
					var index = $(".register_baby_info_table").index(this);
					var babyName = $(this).find("#babyName"+(index+1)).val();
					var babySex = $(this).find(":radio[name='babySex"+(index+1)+"'][checked]").val();
			//		var yearV = $(this).find("#year").val();
			//		var monthV = $(this).find("#month").val();
//					var dayV = $(this).find("#day").val();
					var birthday = $(this).find("#year").val()+"-"+$(this).find("#month").val()+"-"+$(this).find("#day").val();
					arrayObj.push({
						babyName:babyName,
						babySex:babySex,
						birthday:birthday
					});
				});
				family.babyList = arrayObj;
			
				$.ajax({type:'post',
					 url:'register.ajax',  
					 data:{jsonData:escape($.toJSON(family))},
					 success:function(data){
						window.location = "registerResult.ajax";
					 }
					});
			
					
			}
		});
		//初始化省市区
		initProCityDis('province','city','district');
//		得到要添加宝宝的html
		var babyInfoTable = $("#register_baby_info_table_id").html();
//		将初始化在页面中的添加宝宝的html移除
		$("#register_baby_info_table_id").remove();
//		加到wrap中
//		$("#register_baby_info_wrap").append(babyInfoTable);
		//初始化添加宝宝table的参数
//		initBabyInfoTable($(".register_baby_info_table"),1);

		
		//默认选中有宝宝
		$("input[name='hasBaby'][value='0']").attr("checked",true);
		//是否有宝宝
		$("input[name='hasBaby']").click(
			function(){
				var value = $(this).val();
				if(value=='0'){
					$("#register_baby_info_wrap").empty();
					$("#register_add_cancel_baby").css("display","none");
				}else{
//					不能多次点击
					if($(".register_baby_info_table").size()< 1){
						$("#register_baby_info_wrap").append(babyInfoTable);
						yearMonthDay($("#register_birthday"));
						initBabyInfoTable($(".register_baby_info_table"),1);
						$("#register_add_cancel_baby").css("display","");
					}
					
				}
			}
		);
		//增加宝宝
		$("#register_add_baby").click(function(){
			$("#register_baby_info_wrap").append(babyInfoTable);
			var babySize = $(".register_baby_info_table").size();
			$(".register_baby_info_table").each(function(){
				var index = $(".register_baby_info_table").index(this);
				if(index==babySize-1){
					initBabyInfoTable(this,babySize);
				}
				
			}); 
			
			
		});
		//取消宝宝
		$("#register_cancel_baby").click(function(){
			var size = $(".register_baby_info_table").size();
			if(size == 1)return ;
			$(".register_baby_info_table").each(function(){
				var index = $(".register_baby_info_table").index(this);
				if((index+1) == size){
					$(this).remove();
				}
				
			});
		});
		
		
	});
	
	function initBabyInfoTable(obj,babySize){
		var babyName = $(obj).find("input[name='babyName1']");
		babyName.attr("id","babyName"+babySize);
		babyName.attr("name","babyName"+babySize);
		//添加验证规则
		babyName.rules("add",{
							required:true,
							minlength:2,
							maxlength:5,
							chinese:24,
							messages:{
									required:'请填写宝贝的真实名字',
									minlength:'请填写2个以上中文',
									maxlength:'请填写5个以下中文',
									chinese:'请填写2个以上中文'
										}
						});
		
		$(obj).find("input[name='babySex1']").attr('name','babySex'+babySize);
		$(obj).find("input[name='babySex"+babySize+"']").each(function(){
			//默认选中性别男
			if($(this).val()=='M'){
				$(this).attr("checked",true);
			}else{
				$(this).attr("checked",false);
			}
		
		});
		yearMonthDay($(obj).find("#register_birthday"));
		
		
	}
	